from flask_restful import Resource
from flask_restful import marshal, fields

from apps.models.common import City, Letter

city_fields = {
    'id': fields.Integer(attribute='city_id'),
    'parentId': fields.Integer(attribute='parent_id'),
    'regionName': fields.String(attribute='region_name'),
    'cityCode': fields.Integer(attribute='city_code'),
    'pinyin': fields.String,
}

class CitiesResource(Resource):
    def get(self):
        letters = Letter.query.all()
        letters_cities = {}
        letters_cities_fields = {}
        for letter in letters:
            letter_cities = City.query.filter_by(letter_id=letter.id)

            letters_cities[letter.letter] = letter_cities
            letters_cities_fields[letter.letter] = fields.List(fields.Nested(city_fields))
        data = {
            "msg": "ok",
            "status": 200,
            "data": marshal(letters_cities, letters_cities_fields)
        }

        return data
